package com.gs.m_order_list

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.BaseStateCallback
import com.gs.common.constant.StateCode

class OrderListPersistent(context: Context) {

    private val orderListModel by lazy { OrderListModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun getPaidOrdersInfo(username: String, date: String? = null, callback: OnGetOrdersInfoStateCallback) {
        Thread{
            orderListModel.getPaidOrdersInfo(username, date) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetOrdersInfoLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetOrdersInfoSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetOrdersInfoFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun deleteOrder(orderId: Int, callback: OnDeleteOrderStateCallback) {
        Thread{
            orderListModel.deleteOrder(orderId){
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onDeleteOrderLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onDeleteOrderSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onDeleteOrderFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    interface OnDeleteOrderStateCallback: BaseStateCallback {
        fun onDeleteOrderLoading(){}
        fun onDeleteOrderSuccess()
        fun onDeleteOrderFailed()
    }

    interface OnGetOrdersInfoStateCallback: BaseStateCallback {
        fun onGetOrdersInfoLoading(){}
        fun onGetOrdersInfoSuccess()
        fun onGetOrdersInfoFailed()
    }

    companion object {
        private var instance: OrderListPersistent? = null
        fun getInstance(context: Context): OrderListPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = OrderListPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}